package com.china08.yxyapi.api.manage.sch.platform.website.setting;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.sws.SwsSetting;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.SwsSettingRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController("SwsSettingUptSchoolInfoController")
public class SwsSettingUpdSchoolInfoController extends YxyApiBaseController {

    @Autowired
    private SwsSettingRepository swsSettingRepository;

    // 修改学校信息
    @ApiOperation(value = "修改学校信息")
    @RequestMapping(value = "/api_yw/manage/sch/platform/website/setting/upd/schoolinfo", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object precess(HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestBody(required = false) ReqModel reqModel) {

        String schoolId = getProfile(request, response).getSchoolId();
        String schoolName = reqModel.schoolName;
        String description = reqModel.description;
        String telephone = reqModel.telephone;
        String address = reqModel.address;
        String schoolBadge = reqModel.schoolBadge;

        SwsSetting swsSetting = swsSettingRepository.findBySchoolId(schoolId);
        swsSetting.setSchoolName(schoolName);
        swsSetting.setDescription(description);
        swsSetting.setTelephone(telephone);
        swsSetting.setAddress(address);
        swsSetting.setSchoolBadge(schoolBadge);

        swsSettingRepository.save(swsSetting);
        return true;
    }

    protected static class ReqModel extends YxyApiReqModel {
        public String schoolBadge;
        public String schoolName;
        public String description;
        public String telephone;
        public String address;

        @Override
        public void autoCheck() {
            // Assert.isTrue(StringUtils.isNotBlank(schoolBadge), "学校校徽不能为空");
            // Assert.isTrue(StringUtils.isNotBlank(schoolName), "学校名字不能为空");
            // Assert.isTrue(StringUtils.isNotBlank(description), "学校简介不能为空");
            // Assert.isTrue(StringUtils.isNotBlank(telephone), "联系电话不能为空");
            // Assert.isTrue(StringUtils.isNotBlank(address), "联系地址不能为空");
            if (StringUtils.isNotBlank(schoolName)) {
                Assert.isTrue(schoolName.length() < 23 && schoolName.length() > 0, "请输入1-22个字网站名称");
            }
            if (StringUtils.isNotBlank(description)) {
                Assert.isTrue(description.length() < 2001 && description.length() > 0, "请输入1-2000个字的网站简介");
            }
            if (StringUtils.isNotBlank(telephone)) {
                boolean b1 = isMobile(telephone);
                boolean b2 = isPhone(telephone);
                Assert.isTrue(b1 || b2, "请输入有效的电话号码");
            }
        }
    }

    protected static class RespModel extends YxyApiRespModel {

    }

    public static boolean isMobile(String str) {
        Pattern p = null;
        Matcher m = null;
        boolean b = false;
        p = Pattern.compile("^[1][3,4,5,8][0-9]{9}$"); // 验证手机号
        m = p.matcher(str);
        b = m.matches();
        return b;
    }

    public static boolean isPhone(String str) {
        Pattern p1 = null, p2 = null, p3 = null;
        Matcher m1 = null, m2 = null, m3 = null;

        p1 = Pattern.compile("^[0-9]{3,4}-[0-9]{5,10}$");
        p2 = Pattern.compile("^[0-9]{8}$");
        p3 = Pattern.compile("^[0-9]{3,4}-[0-9]{3,4}-[0-9]{3,4}$");

        m1 = p1.matcher(str);
        m2 = p2.matcher(str);
        m3 = p3.matcher(str);
        return (m1.matches() || m2.matches() || m3.matches());
    }
}
